#include <game.h>

logger::logger() : base("logger")
{
	files = new string<IWriteFile*>;
}

logger::~logger()
{
	delete files;
}

int logger::init(coeur* c,IFileSystem* f)
{
	fs = f;
	base::init(0,c);
	return 1;
}

int logger::close()
{
	base::close();
	return 1;
}

int logger::createFile(stringc name, int idClass)
{
	stringc path = "log/";
	path += name + "/" + idClass + ".log";
	IWriteFile* file = fs->createAndWriteFile(path.c_str());
	files->append(file);
	int idLog = files->size()-1;
	write(idLog,stringc("Class : " ) + name);
	write(idLog,stringc("Id :  ") + idClass);
	write(idLog,stringc("Begin log" ));
	write(idLog,stringc("------------------------------------------------------------"));
	if (name != stringc("logger"))
		log( stringc("Logger create a new file for class ") + name + " of id " + idClass + ". The log id is " + idLog);
	else
		write(idLog, stringc("The log id of this logger is ") + idLog);
	return idLog;
}

int logger::write(int idLog,stringc text)
{
	text = stringc(mycore->getTime()/1000) + ":" + text + "\n";
	(*files)[idLog]->write(text.c_str(),text.size());
	return 1;
}

int logger::closeFile(int idLog)
{
	log(stringc("Close the log of id ") + idLog);
	write(idLog,stringc("------------------------------------------------------------"));
	write(idLog,stringc("End log"));
	(*files)[idLog]->drop();
	return 1;
}


bool logger::OnEvent(const SEvent& event)
{
	return false;
}


